/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package initdb.Vista;

import initdb.escritoraSQL;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author joser93
 */
public class Ventana extends JFrame implements ActionListener{

    ArrayList<JTextField> campos;
    String[] acciones={"Conectar","Limpiar","Salir"};
    static Ventana v;
    JPasswordField pass;
    
    public static Ventana getInstance(){
        if(v==null){
            v=new Ventana();
        }
        return v;
    }
    
    private Ventana() throws HeadlessException {
        configInicial();
        configMenu();
        configComponentes();
        setTitle("ORACLE Init DataBase... Inicio");
    }
    
    private void configComponentes(){
        Dimension dimP=new Dimension(150, 30);
        campos=new ArrayList<>();
        setLayout(new BorderLayout());
        
        JPanel f=new JPanel(new BorderLayout());
        JPanel p=new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.add(Box.createRigidArea(new Dimension(30, 30)));
        p.add(barraServ(dimP));
        p.add(Box.createHorizontalGlue());
        p.add(barraUser(dimP));
        p.add(Box.createHorizontalGlue());
        p.add(barraContrasena(dimP));
        p.add(Box.createHorizontalGlue());
        f.add(p, BorderLayout.CENTER);
        f.add(new JLabel(new ImageIcon(getClass().getResource("/initdb/Vista/Imagenes/imaInicio2.png"))), BorderLayout.NORTH);
        f.add(botonesFinal(dimP), BorderLayout.SOUTH);
        add(f, BorderLayout.CENTER);
    }
    
    private void configMenu(){
        JMenuBar barra=new JMenuBar();
        setJMenuBar(barra);
        JMenu m=new JMenu("General");
        barra.add(m);
        JMenuItem item;
        for(String s:acciones){
            item=new JMenuItem(s);
            m.add(item);
            item.addActionListener(this);
        }
    }
    
    private JPanel botonesFinal(Dimension dim){
        JPanel l=new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton b;
        for(String s:acciones){
            b=new JButton(s);
            b.addActionListener(this);
            l.add(b);
        }
        return l;
    }
    
    private JPanel barraContrasena(Dimension dim){
        JPanel l=new JPanel(new GridLayout(1, 2));
        l.add(new JLabel("Contraseña:"));
        pass=new JPasswordField();
        pass.setPreferredSize(dim);
        l.add(pass);
        return l;
    }
    
    private JPanel barraUser(Dimension dimP){
        JPanel l=new JPanel(new GridLayout(1, 2));
        l.add(new JLabel("Usuario:"));
        JTextField c=new JTextField();
        c.setPreferredSize(dimP);
        campos.add(c);
        l.add(c);
        return l;
    }
    
    private JPanel barraServ(Dimension dimP){
        JPanel l=new JPanel(new GridLayout(1, 4));
        l.add(new JLabel("Servidor:"));
        JTextField c=new JTextField();
        c.setPreferredSize(dimP);
        campos.add(c);
        l.add(c);
        l.add(new JLabel("Puerto:"));
        c=new JTextField();
        campos.add(c);
        c.setPreferredSize(dimP);
        l.add(c);
        return l;
    }
    
    private void configInicial(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 400));
        setLocationRelativeTo(null);
        setResizable(false);
        
    }
    
    public void mostrar(){
        pack();
        setVisible(true);
    }
    
    public static void main(String[] args){
        Ventana.getInstance().mostrar();
        escritoraSQL prueba = new escritoraSQL();
        prueba.escribir();
        
    }
    
    void limpiar(){
        for(JTextField t:campos){
            t.setText("");
        }
        pass.setText("");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand()){
            case "Salir":{
                System.exit(0);
            }break;
            case "Limpiar":{
                limpiar();
            }break;
            case "Conectar":{
                System.out.println("Conectando a la base de datos...");
            }break;
            default:
                throw new UnsupportedOperationException("Accion "+ e.getActionCommand() + " no implementado");
        }
    }

}
